package com.fenghao.subject.application.controller;

import com.alibaba.fastjson.JSON;
import com.fenghao.subject.application.convert.SubjectAnswerDtoConvert;
import com.fenghao.subject.application.convert.SubjectInfoDtoConvert;
import com.fenghao.subject.application.dto.SubjectAnswerDTO;
import com.fenghao.subject.application.dto.SubjectInfoDTO;
import com.fenghao.subject.common.entity.Result;
import com.fenghao.subject.domain.entity.SubjectAnswerBO;
import com.fenghao.subject.domain.entity.SubjectInfoBO;
import com.fenghao.subject.domain.service.SubjectInfoDomainService;
import com.google.common.base.Preconditions;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * SubjectInfoController ：题目信息相关！！！
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/subject/info")
public class SubjectInfoController {

    private final SubjectInfoDomainService subjectInfoDomainService;

    /**
     * 新增题目
     */
    @PostMapping("/add")
    public Result<Boolean> addSubject(@RequestBody SubjectInfoDTO subjectInfoDTO) {

        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectInfoController 方法 addSubject ：校验添加题目入参：" + "SubjectInfoDTO:{},", JSON.toJSONString(subjectInfoDTO));
            }

            // 入参校验
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectType(), "题目类型不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectScore(), "题目分数不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectAnswer(), "题目答案不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectDifficult(), "题目难度不能为空");
            Preconditions.checkArgument(!CollectionUtils.isEmpty(subjectInfoDTO.getLabelIds()), "标签id不能为空");
            Preconditions.checkArgument(!CollectionUtils.isEmpty(subjectInfoDTO.getCategoryIds()), "分类id不能为空");
            Preconditions.checkArgument(StringUtils.isNotBlank((subjectInfoDTO.getSubjectName())), "题目名称不能为空");
            // Bean转换
            List<SubjectAnswerDTO> subjectAnswerDtoList = subjectInfoDTO.getOptionsList();
            List<SubjectAnswerBO> subjectAnswerBoList = SubjectAnswerDtoConvert.INSTANCE.convertDtoListToBoList(subjectAnswerDtoList);
            SubjectInfoBO subjectInfoBO = SubjectInfoDtoConvert.INSTANCE.convertDtoToBo(subjectInfoDTO);
            subjectInfoBO.setOptionsList(subjectAnswerBoList);

            subjectInfoDomainService.addSubject(subjectInfoBO);

            return Result.ok(true);
        } catch (Exception e) {
            log.error("SubjectInfoController 方法 :addSubject:error:{}",
                    e.getMessage(), e);
            return Result.fail("添加题目失败");
        }
    }
}
